package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjc.drygoods.ware.entity.PurchaseEntity;
import com.yjc.drygoods.ware.service.PurchaseService;
import com.yjc.drygoods.ware.vo.MergeVo;
import com.yjc.drygoods.ware.vo.PurchaseDoneVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 采购信息
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:23:36
 */
@Slf4j
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;


    /**
     * 获取所有已领取的采购单信息以及下辖的所有采购需求
     */
    @GetMapping("/get/all/haved/purchase")
    public R getAllHavedPurchaseWithDetail(@RequestParam Map<String, Object> params) {
        PageUtils list = purchaseService.getAllHavedPurchaseWithDetail(params);
        log.info("list:{}", list);
        return R.ok().put("data", list);
    }

    /**
     * 获取所有分配给指定id用户的采购单信息以及下辖的所有采购需求
     */
    @GetMapping("/get/all/assigned/purchase")
    public R getAllAssignedPurchaseWithDetail(@RequestParam Map<String, Object> params) {
        PageUtils list = purchaseService.getAllAssignedPurchaseWithDetail(params);
        log.info("list:{}", list);
        return R.ok().put("data", list);
    }


    /**
     * /ware/purchase/done
     * 完成采购
     */
    @PostMapping("/done")
    public R donePurchase(@RequestBody PurchaseDoneVo purchaseDoneVo) {
        log.info("purchaseDoneVo:{}",purchaseDoneVo);
        purchaseService.donePurchase(purchaseDoneVo);
        return R.ok();
    }

    /**
     * /ware/purchase/received
     * 领取采购单
     */
    @PostMapping("/received")
    public R receivePurchase(@RequestBody String ids) {
        log.info("ids:{}", ids);
        JSONObject msgEncap = JSON.parseObject(ids);
        List<Long> msgList = JSON.parseArray(msgEncap.getString("ids"), Long.class);
        log.info("ids:{}", msgList);
        purchaseService.receivePurchase(msgList);
        return R.ok();
    }

    /**
     * /ware/purchase/unreceive/list
     * 查询未领取的采购单
     */
    @GetMapping("/unreceive/list")
    public R getUnreceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPageUnreceiveList(params);
        return R.ok().put("page", page);
    }

    /**
     * /ware/purchase/merge
     * 合并采购需求
     */
    @PostMapping("/merge")
    public R saveMerge(@RequestBody MergeVo mergeVo) {
        purchaseService.saveMerge(mergeVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
